    package cn.pconline.autoclub.service.bbssync;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;

import cn.pconline.autoclub.entity.Club;
import cn.pconline.autoclub.entity.ClubIntegral;
import cn.pconline.autoclub.entity.ClubMember;
import cn.pconline.autoclub.entity.User;
import cn.pconline.autoclub.entity.UserLimit;
import cn.pconline.autoclub.service.DynaService;
import cn.pconline.autoclub.service.PetrolService;
import cn.pconline.autoclub.service.UserLimitService;

import com.alibaba.fastjson.JSONObject;

/**
 * @author zhouruxuan
 * @date 2014年12月4日 上午11:16:50
 */
public class SyncRecoverReplyService extends BbsSyncPetrolService {
	@Autowired
	private DynaService dynaService;
	@Autowired
	private PetrolService petrolService;
	
	@Autowired
    private UserLimitService limitService;
    
    @Override
    protected int addPetroltoMember(ClubMember clubMember, JSONObject msg) {
        ClubIntegral clubIntegral = getClubIntegral(msg);
        String reason = clubIntegral.getName() + "(topicId-" + getTopicId(msg) + ", postId-" + getPostId(msg) + ")";
        petrolService.addOnlyPersonWithinLimit(clubMember, (int)clubIntegral.getIntegralId(), clubIntegral.getPetrol(), reason, true);
        return clubIntegral.getPetrol();
    }

    @Override
    protected int addPetroltoClub(Club club, JSONObject msg) {
        ClubIntegral clubIntegral = getClubIntegral(msg);
        if(clubIntegral == null) {
            return 0;
        }
        long userId = getUserId(msg);
        UserLimit userLimit = null;
        if(userId > 0) {
            ClubMember clubMember = memberService.findClubMemberByUserId(userId);
            if(clubMember != null) {
                userLimit = limitService.getMemberLimit(clubMember.getId());
            }
        }
        if(userLimit == null || userLimit.getPetrol() >= UserLimit.DAILY_PERSON_LIMIT) {
            log.info("user[{}] reach the limit[{}]petrol, no longer add petrol to club!", userId, UserLimit.DAILY_PERSON_LIMIT);
            return 0;
        }
        String reason = clubIntegral.getName() + "(topicId-" + getTopicId(msg) + ", postId-" + getPostId(msg) + ")";
        petrolService.addToClubWithinLimit(club.getClubId(), (int)clubIntegral.getIntegralId(), clubIntegral.getPetrol(), reason, true);
        return clubIntegral.getPetrol();
    }
    
    @Override
    protected long getUserId(JSONObject msg) {
        return msg.getLongValue("postAuthorId");
    }
    
	@Override
	public void process(JSONObject msg, ClubMember clubMember) {
	    
		//验证用户
	    long userId = getUserId(msg);
	    if (userId <= 0L) {
            log.info("not userId-{}", userId);
            return;
        }

        //判断用户是否登录
        User user = userService.authUser(userId);
        if (user == null || user.getUid() == 0L) {
            log.info("not found user-{}", userId);
            return;
        }

        //恢复回帖
        long topicId = msg.getLongValue("topicId");
        Date createAt = msg.getDate("topicCreateAt");
        long postId = msg.getLongValue("postId");
        dynaService.rollbackDynaReply(topicId, createAt, postId);
	}
	
	private int getAgent(JSONObject msg) {
	    return msg.getIntValue("agent");
	}

	private ClubIntegral getClubIntegral(JSONObject msg) {
        if (getAgent(msg) <= 0) {
            return petrolService.getById(PetrolService.ADD_PERSONAL_PETROL_ITEM_POST);
        } else {
            return petrolService.getById(PetrolService.ADD_PERSONAL_PETROL_ITEM_POST_WAP);
        }
	}
	
	private long getTopicId(JSONObject msg) {
	    return msg.getLongValue("topicId");
	}
	
	private long getPostId(JSONObject msg) {
	    return msg.getLongValue("postId");
	}
}
